package org.jeecg.modules.kuybFacilitator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.jeecg.modules.kuybFacilitator.entity.KuybFacilitator;
import org.jeecg.modules.kuybFacilitator.mapper.KuybFacilitatorMapper;
import org.jeecg.modules.kuybFacilitator.service.IKuybFacilitatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: kuyb_facilitator
 * @Author: jeecg-boot
 * @Date: 2021-09-22
 * @Version: V1.0
 */
@Service
public class KuybFacilitatorServiceImpl extends ServiceImpl<KuybFacilitatorMapper, KuybFacilitator> implements IKuybFacilitatorService {

    @Autowired
    KuybFacilitatorMapper facilitatorMapper;

    @Override
    public List<KuybFacilitator> findFacilitatorListByPoint(KuybFacilitator kuybFacilitator,int pageNo,int pageSize) {
       /* QueryWrapper queryWrapper= new QueryWrapper<KuybFacilitator>().like(StringUtils.isNotEmpty(kuybFacilitator.getType()), "type", kuybFacilitator.getType());
        Page<KuybFacilitator> page = new Page<KuybFacilitator>(pageNo, pageSize);
        IPage<KuybFacilitator> pageList = facilitatorMapper.findTopFacilitatorByType(pageNo,pageSize,kuybFacilitator.getType());//
*/
        List<KuybFacilitator> facilitators =facilitatorMapper.findTopFacilitatorByType(pageNo,pageSize,kuybFacilitator.getType(),kuybFacilitator.getLatitudes(),kuybFacilitator.getLongitudes());//
        for (int i = 0; facilitators != null && i < facilitators.size(); i++) {
            // 判空
            if(facilitators.get(i).getLatitudes() == null || facilitators.get(i).getLongitudes() == null)
                continue;
            GlobalCoordinates source = new GlobalCoordinates(kuybFacilitator.getLatitudes().doubleValue(), kuybFacilitator.getLongitudes().doubleValue());
            GlobalCoordinates target = new GlobalCoordinates(facilitators.get(i).getLatitudes().doubleValue(), facilitators.get(i).getLongitudes().doubleValue());
            double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
            System.out.println("Sphere坐标系计算结果：" + meter1 + "米");
            facilitators.get(i).setMeter(meter1);

            BigDecimal b = new   BigDecimal(meter1 / 1000);
            double f2 = b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();

            facilitators.get(i).setMeterNode("距您： " + f2 + " km");
        }
//        facilitators.stream().sorted(Comparator.comparing(KuybFacilitator::getMeter));
//        facilitators.sort((o1, o2)->o1.getMeter().compareTo(o2.getMeter()));
        facilitators.sort(Comparator.comparing(KuybFacilitator::getMeter));
//        pageList.setRecords(facilitators);

        return facilitators;
    }

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }

}
